<?php
namespace App;
defined('IN_IA') or exit('Access Denied');

use Model\CloudMod;

class Cloud extends Action
{
    private $Cloud = null;
    public function __construct()
    {
        parent::__construct();
        $this->Cloud = new CloudMod();
    }

    /**
     * 获取本地云服务数据
     * @param string module 模块标识
     * @param string wx_appid 公众号id
     */
    public function getCloud()
    {
        $_cloud = $this->Cloud->findOne(array("module"=>$this->_gpc["module"],"uniacid"=>$this->_uniacid,"wx_appid"=>$this->_gpc["wx_appid"]),array("identity","past_time","authority","check_time"));
        if($_cloud && $_cloud["identity"]){
            $_cloud["past_date"] = date("Y-m-d H:i:s",$_cloud["past_time"]);
            $this->_result = array("status"=>"success","data"=>$_cloud);
        }
        $this->result($this->_result);
    }

    /**
     * 注册本地云服务
     * @param string identity 唯一标识符
     * @param string module 模块名称
     * @param string wx_appid 公众号appid
     */
    public function register()
    {
        $_cloud = $this->Cloud->findOne(array("identity"=>$this->_gpc["identity"]),array("id"));
        if(!$_cloud){
            $_data = array(
                "uniacid"=>$this->_uniacid,
                "identity"=>$this->_gpc["identity"],
                "module"=>$this->_gpc["module"],
                "register_time"=>TIMESTAMP,
                "wx_appid"=>$this->_gpc["wx_appid"]
            );
            if($this->Cloud->insert($_data)){
                $this->_result = array("status"=>"success");
            }
        }else{
            $this->_result = array("status"=>"success");
        }
        $this->result($this->_result);
    }

    /**
     * 设置授权码
     * @param string identity 标识符
     * @param string authority 授权码
     * @param int check_time 验证时间
     * @param int past_time 过期时间
     */
    public function setAuthority()
    {
        $_data = array("authority"=>$this->_gpc["authority"],"past_time"=>$this->_gpc["past_time"]);
        if(isset($this->_gpc["check_time"])) $_data["check_time"] = $this->_gpc["check_time"];
        if($this->Cloud->update($_data,array("identity"=>$this->_gpc["identity"]))){
            $this->_result = array("status"=>"success");
        }
        $this->result($this->_result);
    }
}